package com.by.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StockController {


    @SentinelResource(value = "库存",fallback = "fallback")
    @GetMapping("/stock")
    public String get() {
        int tmp =(int) (Math.random() * 1000);
        System.out.println("耗费时间："+tmp);
       /* try {
            Thread.sleep(tmp);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }*/
        if(tmp%2==0){
            throw new RuntimeException("异常");
        }
        return "库存";
    }

    public String fallback(Throwable e){
        System.out.println("降级处理："+e.getMessage());
        return "error";
    }
}
